Android

Check Box

 

 

chk_red.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

    }
});

chk_blue.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(((CompoundButton) view).isChecked()){
            Log.d(TAG, "Checked");
        } else {
            Log.d(TAG, "Un-Checked");
        }
    }
});

https://stackoverflow.com/questions/8386832/android-checkbox-listener

 

 

 


 

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CheckboxActivity">

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="결과 텍스트"
        android:textSize="36sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_result" />

    <CheckBox
        android:id="@+id/chk_red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="빨강"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/chk_blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="파랑"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/chk_red" />

    <CheckBox
        android:id="@+id/chk_green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="초록"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/chk_blue" />

    <Button
        android:id="@+id/btn_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="선택 완료"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/chk_green" />

</androidx.constraintlayout.widget.ConstraintLayout>

 


 

 

public class CheckboxActivity extends AppCompatActivity {

    private CheckBox chk_red, chk_blue, chk_green;
    private TextView tv_result;
    private Button btn_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkbox);

        chk_red = findViewById(R.id.chk_red);
        chk_blue = findViewById(R.id.chk_blue);
        chk_green = findViewById(R.id.chk_green);
        tv_result = findViewById(R.id.tv_result);
        btn_result = findViewById(R.id.btn_result);

        btn_result.setOnClickListener(new View.OnClickListener() { // 결과 버튼을 클릭했을때 액션
            @Override
            public void onClick(View view) {
                String str_result = ""; // String 값 초기화
                if(chk_red.isChecked()) { // 빨강 체크 박스에 체크가 되어있다면...
                    str_result += chk_red.getText().toString();
                }
                if(chk_blue.isChecked()) { // 파랑 체크 박스에 체크가 되어있다면...
                    str_result += chk_blue.getText().toString();
                }
                if(chk_green.isChecked()) { // 초록 체크 박스에 체크가 되어있다면...
                    str_result += chk_green.getText().toString();
                }
                tv_result.setText(str_result); // 체크박스에 체크되어있던 값을 String으로 텍스트뷰에 출력.
            }
        });
    }
}

 

 


 

안드로이드 앱 만들기 #37 Check Box (옵션 선택 버튼)
https://www.youtube.com/watch?v=QNmBB9ibOUI

홍드로이드 깃헙
https://github.com/hongdroid94/37_CheckBox

Related posts

Leave a Comment